package com.wanma.recuit.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookiesUtils {
    public static void write(String name, String value, String path, int maxAge, HttpServletResponse response){
        Cookie cookie = new Cookie(name,value);
        //设置起作用的路径
        cookie.setPath(path);
        //设置过期时间
        cookie.setMaxAge(maxAge);
        //写到客户端
        response.addCookie(cookie);
    }
    public  static String read(String name, HttpServletRequest request){
        String value = null;
        Cookie[]  cookies = request.getCookies();
        if(cookies!=null){
            for (Cookie cookie : cookies) {
                if(cookie.getName().equals(name)){
                    value = cookie.getValue();
                    break;
                }
            }
        }
        return value;
    }
}
